package cn.kgc.day04.servlet;

import cn.kgc.day04.entity.Score;
import cn.kgc.day04.service.ScoreService;
import cn.kgc.day04.service.impl.ScoreServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/10/11
 * @description  控制层   接收请求  返回结果
 */
@WebServlet("/score")
public class ScoreServlet extends BaseServlet {

    private ScoreService scoreService = new ScoreServiceImpl();

    // 编辑分数
    protected void editScore(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        // 获取请求参数    String   参数类型单一
        String name = req.getParameter("name");
        String score = req.getParameter("score");
        String course = req.getParameter("course");
        String id = req.getParameter("id");

        // 参数封装
        Score s = new Score();
        if (score!=null){
            s.setScore(Integer.valueOf(score));
        }

        if (id!=null){
            s.setId(Integer.valueOf(id));
        }
        s.setName(name);
        s.setCourse(course);

         Boolean result =  scoreService.editScore(s);
        // 判定业务层结果 实现页面跳转
        if (result){   // 将页面跳转到成绩列表页   score.jsp
            resp.sendRedirect("/javaweb/score?method=scoreList");
        }else{  //  添加页
            req.setAttribute("info","成绩编辑失败...");
            req.getRequestDispatcher("/editScore.jsp").forward(req,resp);
        }

    }

    // 删除分数
    protected void deleteScore(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        String id = req.getParameter("id");
        Integer dbId = null;
        if (id!=null){
            dbId = Integer.valueOf(id);
        }
        Boolean result  =  scoreService.deleteById(dbId);
        HttpSession session = req.getSession();

        if (result){
            session.setAttribute("info","数据删除成功");
        }else{
            session.setAttribute("info","数据删除失败");
        }
        resp.sendRedirect(req.getContextPath()+"/score?method=scoreList");

    }

    // 查询分数
    protected void scoreList(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

       List<Score> scoreList =  scoreService.scoreList();

       //  将scoreList -> score.jsp
        req.setAttribute("scoreList",scoreList);
        req.getRequestDispatcher("/score.jsp").forward(req,resp);
    }


    // 根据id 查询一个成绩信息
    protected void selectOne(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        String id = req.getParameter("id");
        Integer dbId = null;
        if (id!=null){
            dbId = Integer.valueOf(id);
        }

        Score score =  scoreService.selectOne(dbId);
       //  将scoreList -> score.jsp
        HttpSession session = req.getSession();
        session.setAttribute("score",score);

        // req.getContextPath()  动态获取虚拟路径
       resp.sendRedirect(req.getContextPath()+"/editScore.jsp");
    }

    // 添加分数
    protected void addScore(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        // 获取请求参数    String   参数类型单一
        String name = req.getParameter("name");
        String score = req.getParameter("score");
        String course = req.getParameter("course");

        // 参数封装
        Score s = new Score();
        if (score!=null){
            s.setScore(Integer.valueOf(score));
        }
        s.setName(name);
        s.setCourse(course);

        // 调用业务层 实现数据添加
        Boolean  result =   scoreService.addScore(s);
        // 判定业务层结果 实现页面跳转
        if (result){   // 将页面跳转到成绩列表页   score.jsp
            resp.sendRedirect("/javaweb/score?method=scoreList");
        }else{  //  添加页
            req.setAttribute("info","成绩添加失败...");
            req.getRequestDispatcher("/addScore.jsp").forward(req,resp);
        }

    }
}
